package com.yanqu.road.dao.impl.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.config.parergon.DiscountShopConfig;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;

public class DiscountShopConfigDaoImpl extends TempDao {
    public Map<Integer, DiscountShopConfig> getDiscountShopConfig() {
        Map<Integer, DiscountShopConfig> dataMap = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_p_discount_shop";
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                DiscountShopConfig data = new DiscountShopConfig();
                data.setId(rs.getInt("id"));
                data.setPrice(rs.getLong("price"));
                data.setContent(rs.getString("content"));
                data.setParamList(StringUtils.stringToIntegerList(rs.getString("params"), "\\|"));
                dataMap.put(data.getId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }

        return dataMap;
    }
}
